package com.imooc.controller;

import com.imooc.base.BaseInfoProperties;
import com.imooc.entity.bo.VlogBo;
import com.imooc.grace.result.GraceJSONResult;
import com.imooc.service.MsgService;
import com.imooc.service.VlogService;
import com.imooc.utils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/vlog")
@Slf4j
public class VlogController extends BaseInfoProperties {
    @Autowired
    private VlogService vlogService;

    @PostMapping("/publish")
    public GraceJSONResult publishVlog(@RequestBody VlogBo vlogBo){
        vlogService.publishVlog(vlogBo);
        return GraceJSONResult.ok();
    }

    @GetMapping("/indexList")
    public GraceJSONResult indexList(@RequestParam(value = "search",defaultValue = "")String search,
                                     @RequestParam(value = "userId")String userId,
                                     @RequestParam("page")Integer page,
                                     @RequestParam("pageSize")Integer pageSize){
        log.info("search = " + search);
        if(page==null){
            page = COMMON_START_PAGE;
        }
        if(pageSize==null){
            pageSize = COMMON_PAGE_SIZE;
        }
        PagedGridResult pagedGridResult = vlogService.findIndexList(userId,search,page,pageSize);
        return GraceJSONResult.ok(pagedGridResult);
    }
    @GetMapping("/detail")
    public GraceJSONResult detail(@RequestParam String userId,
                                  @RequestParam String vlogId) {
        log.info("=====>{}",vlogService.getVlogDetailById(userId, vlogId));
        return GraceJSONResult.ok(vlogService.getVlogDetailById(userId, vlogId));
    }
    // Post changeToPrivate userId,vlogId,yesOrNo
    @PostMapping("/changeToPrivate")
    public GraceJSONResult changeToPrivate(@RequestParam("userId")String userId,
                                           @RequestParam("vlogId")String vlogId){
        vlogService.changeToPrivate(userId,vlogId);
        return GraceJSONResult.ok();
    }
    // Post changeToPublic
    @PostMapping("/changeToPublic")
    public GraceJSONResult changeToPublic(@RequestParam("userId")String userId,
                                          @RequestParam("vlogId")String vlogId){
        vlogService.changeToPublic(userId,vlogId);
        return GraceJSONResult.ok();
    }
    /**
     * myPublicList
     * myPrivateList
     * myLikedList
     */
    @GetMapping("/myPublicList")
    public GraceJSONResult getMyPublicList(@RequestParam("userId")String userId,
                                           @RequestParam("page")Integer page,
                                           @RequestParam("pageSize")Integer pageSize){
        if(page==null){
            page = COMMON_START_PAGE;
        }
        if(pageSize==null){
            pageSize = COMMON_PAGE_SIZE;
        }
        PagedGridResult pagedGridResult = vlogService.findMyPublicList(userId,page,pageSize);
        return GraceJSONResult.ok(pagedGridResult);
    }
    @GetMapping("/myPrivateList")
    public GraceJSONResult getMyPrivateList(@RequestParam("userId")String userId,
                                           @RequestParam("page")Integer page,
                                           @RequestParam("pageSize")Integer pageSize){
        if(page==null){
            page = COMMON_START_PAGE;
        }
        if(pageSize==null){
            pageSize = COMMON_PAGE_SIZE;
        }
        PagedGridResult pagedGridResult = vlogService.findMyPrivateList(userId,page,pageSize);
        return GraceJSONResult.ok(pagedGridResult);
    }
    @PostMapping("/like")
    public GraceJSONResult likeVlog(@RequestParam("userId")String userId,
                                    @RequestParam("vlogerId")String vlogerId,
                                    @RequestParam("vlogId")String vlogId){
        if(userId==null||vlogerId==null||vlogId==null){
            return GraceJSONResult.errorMsg("参数不能为空");
        }
        vlogService.likeVlog(userId,vlogId);

        redis.increment(REDIS_VLOG_BE_LIKED_COUNTS+":"+vlogId,1);
        redis.increment(REDIS_VLOGGER_BE_LIKED_COUNTS+":"+vlogerId,1);
        // 我点赞的视频
        redis.set(REDIS_USER_LIKE_VLOG+":"+userId+":"+vlogId,"1");
        return GraceJSONResult.ok();
    }
    @PostMapping("/unlike")
    public GraceJSONResult unlikeVlog(@RequestParam("userId")String userId,
                                    @RequestParam("vlogerId")String vlogerId,
                                    @RequestParam("vlogId")String vlogId){
        if(userId==null||vlogerId==null||vlogId==null){
            return GraceJSONResult.errorMsg("参数不能为空");
        }
        vlogService.unlikeVlog(userId,vlogerId);

        redis.decrement(REDIS_VLOG_BE_LIKED_COUNTS+":"+vlogId,1);
        redis.decrement(REDIS_VLOGGER_BE_LIKED_COUNTS+":"+vlogerId,1);
        // 我点赞的视频移除关系
        redis.del(REDIS_USER_LIKE_VLOG+":"+userId+":"+vlogId);
        return GraceJSONResult.ok();
    }
    @PostMapping("/totalLikedCounts")
    public GraceJSONResult totalLikedCounts(@RequestParam("vlogId")String vlogId){
        String countStr = redis.get(REDIS_VLOG_BE_LIKED_COUNTS + ":" + vlogId);
        if(StringUtils.isBlank(countStr)){
            countStr = "0";
        }
        return GraceJSONResult.ok(Integer.valueOf(countStr));
    }
    @GetMapping("/myLikedList")
    public GraceJSONResult myLikedList(@RequestParam("userId")String userId,
                                       @RequestParam(value = "page",defaultValue = "0")Integer page,
                                       @RequestParam(value = "pageSize",defaultValue = "")Integer pageSize){

        return GraceJSONResult.ok(vlogService.getMyLikedVlogList(userId,page,pageSize));
    }
    @GetMapping("/followList")
    public GraceJSONResult followList(@RequestParam("myId")String myId,
                                       @RequestParam(value = "page",defaultValue = "1")Integer page,
                                       @RequestParam(value = "pageSize",defaultValue = "1")Integer pageSize){

        return GraceJSONResult.ok(vlogService.getMyFollowVlogList(myId,page,pageSize));
    }
    @GetMapping("/friendList")
    public GraceJSONResult friendList(@RequestParam("myId")String myId,
                                      @RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "pageSize",defaultValue = "1")Integer pageSize){
        PagedGridResult pagedGridResult = vlogService.getFriendVlogList(myId,page,pageSize);
        return GraceJSONResult.ok(pagedGridResult);
    }
}
